package cn.ac.aircas.system.service.impl;

import cn.ac.aircas.system.domain.RoadConditionPoint;
import cn.ac.aircas.system.domain.vo.RoadConditionPointVO;
import cn.ac.aircas.system.mapper.RoadPointServiceMapper;
import cn.ac.aircas.system.mapper.SysRegionMapper;
import cn.ac.aircas.system.service.IRoadPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoadPointServiceImpl implements IRoadPointService {
    @Autowired
    private RoadPointServiceMapper roadPointServiceMapper;

    @Autowired
    private SysRegionMapper sysRegionMapper;

    @Override
    public int updateRoadPointConditionStatus(RoadConditionPoint road) {
        return roadPointServiceMapper.updateRoadPointConditionStatus(road);
    }

    @Override
    public List<RoadConditionPoint> selectRoadPointConditionList(RoadConditionPoint road) {
        return roadPointServiceMapper.selectRoadPointConditionList(road);
    }

    @Override
    public RoadConditionPoint getRoadPoint(Long conditionId) {
        return roadPointServiceMapper.selectRoadPointById(conditionId);
    }

    @Override
    public int deleteRoadPointByIds(Long[] conditionIds) {
        return roadPointServiceMapper.deleteRoadPointByIds(conditionIds);
    }

    @Override
    public Map<String, Integer> getUndergroundNum(String cityName) {
        String tabName = "road_condition_point";
        if (cityName != null && !cityName.isEmpty()) {
            tabName = "road_condition_point_" + cityName;
        }
        return roadPointServiceMapper.selectUndergroundNum(tabName);
    }

    @Override
    public List<RoadConditionPointVO> getUnderHollowStat(Map<String, Object> param) {
        String tabName = "road_condition_point";
        if (param != null || param.get("cityName") != null && !param.get("cityName").toString().isEmpty()) {
            tabName = "road_condition_point_" + param.get("cityName");
        }
        param.put("tabName", tabName);
        return roadPointServiceMapper.selectUnderHollowStat(param);
    }

    @Override
    public List<Map> getHollowsInRegion(String cityName) {
        String tabName = "road_condition_point";
        if (cityName != null && !cityName.isEmpty()) {
            tabName = "road_condition_point_" + cityName;
        }
        List<Map> list = roadPointServiceMapper.selectHollowsInRegion(tabName);
        List<Map> res = new ArrayList<>();
        res.addAll(list);

        List<String> regioned = new ArrayList<>();
        list.forEach(item -> {
            regioned.add(item.get("name").toString());
        });

        List<String> districts = sysRegionMapper.getDistrictsByCityName(cityName);

        for (String region : districts) {
            if (!regioned.contains(region)) {
                Map<String, Object> map = new HashMap<String, Object>() {{
                    put("name", region);
                    put("hollow", 0);
                    put("pip", 0);
                    put("road", 0);
                    put("value", 0);
                }};
                res.add(map);
            }
        }
        return res;
    }
}
